package LeetCode.month02.day55;

import java.util.TreeSet;

public class No236 {
    TreeNode res = null;
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        dfs(root,p.val,q.val);
        return res;
    }

    public boolean dfs(TreeNode root, int p ,int q){
        if(root == null) return false;
        boolean lson = dfs(root.left,p,q);
        boolean rson = dfs(root.right,p,q);
        if((lson&&rson)||((root.val==p||root.val==q)&&(lson||rson))){
            res = root;
        }
        return lson||rson||root.val ==p||root.val==q;
    }
}
